<?
require("includes/funciones.inc.php");
include_once("includes/phpmailer/class.phpmailer.php");

function sendMailContact()
{
	session_start();
	
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$mail = new PHPMailer();
	$mail->IsSMTP();
	
	$mail->Helo = "www.larctest.com";
	$mail->SMTPAuth = true;
	$mail->SMTPKeepAlive = true; 
	$mail->Host = "mail.larctest.com";
	$mail->Port = 587;
	
	$mail->Username = "info@larctest.com"; 
	$mail->Password = "Cecilia2014";
	$mail->SetFrom('info@larctest.com', 'LARCTEST');
	
	$mail->Timeout=1;
	$mail->IsHTML(true);
	
	$mail->AddAddress("info.larctest@gmail.com");

	$mail->Subject = $_REQUEST['subject'];
	$mail->Body = "Nueva consulta de LARCTEST.COM.<br><br>
				<b>Nombre: </b>" . $_REQUEST['name'] . "<br>
				<b>EMail: </b>" . $_REQUEST['email'] . "<br>
				<b>Telefono: </b>" . $_REQUEST['phone'] . "<br>
				<b>Mensaje: </b>" . $_REQUEST['message'] . "<br>
				<br><br>";

	if($mail->Send()){
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages["Web.MailMsgOK"];
	}else{
		$arrJSON->status = "ERR";
		$arrJSON->msg = $a_languages["Web.MailMsgError"];
	}
	
	return json_encode($arrJSON);
}

function validaUsuario() {
	
	session_start();
	
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	if(!empty($_REQUEST['userName']))
	{
		
		$mcrypt = new MCrypt();
		$encryptedPass = $mcrypt->encrypt($_REQUEST["password"]);
				
		$strSQL = "SELECT * FROM users WHERE userName = '".$_REQUEST['userName']."' AND userPassword = '".$encryptedPass."' AND userState NOT IN ('$stateInactive','$stateDeleted')";
		
		$rsUser = $DB->Execute($strSQL); 
		if (!$rsUser->EOF)
		{
			if(date("Ymd", strtotime($rsUser->fields("expirationDate"))) < date("Ymd")) {
				$arrJSON->status = "FAIL";
				$arrJSON->msg = $a_languages["Common.ErrorExpirationDate"];
				
			}
			else {
				$_SESSION['idUser'] = $rsUser->fields('idUser');
				$_SESSION['idTypeUser'] = $rsUser->fields("idTypeUser");
				$_SESSION['userCompleteName'] = $rsUser->fields("userCompleteName");
				
				$token = $mcrypt->encrypt($rsUser->fields("expirationDate")."|".$rsUser->fields("userCompleteName"));
				
				$arrJSON->status = "GRANT";
				$arrJSON->token = $token;
			}
		}
		else {
			$arrJSON->status = "FAIL";
			$arrJSON->msg = $a_languages["Common.ErrorUserLogin"];
		}
		
		return json_encode($arrJSON);
	}
}

switch($_REQUEST['accion'])
{
	case "sendMailContact":
		echo sendMailContact();
		break;
	case "validaUsuario":
		echo validaUsuario();
		break;
}

?>